package com.cbh.amadeus.client.handler;

import com.cbh.amadeus.client.utils.ChannelReference;
import com.cbh.amadeus.common.enums.MessageType;
import com.cbh.amadeus.common.protocol.AmadeusMessage;
import com.cbh.amadeus.common.protocol.AmadeusMessageWrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ChenBiHui
 * @date 2025/5/22
 */
@Slf4j
public class OnlyConnectBackHandler extends SimpleChannelInboundHandler<AmadeusMessageWrap> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, AmadeusMessageWrap msg) {
        MessageType type = MessageType.find(msg.getType());
        if (type == MessageType.ONLY_CONNECT_BACK) {
            AmadeusMessage message = msg.getMessage();
            log.info("===== [ONLY CONNECT] Respond has back =====");
            ChannelReference.set(ctx.channel(), message.getProvider());
            return;
        }
        ctx.fireChannelRead(msg);
    }
}
